package com.example.administrator.footballgamecollectandanalyze;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import tool.MyApplication;


public class CreateTeam extends AppCompatActivity {
    private Button CreatTeam;
    private String Mana_id;
    TextView TeamName;
    TextView TeamCode;

    String url = "http://112.74.26.238:8080/football/myteam";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_team);

        CreatTeam =(Button) findViewById(R.id.CreatTeam);
        TeamName = (TextView)findViewById(R.id.TeamName);
        TeamCode = (TextView)findViewById(R.id.code);

        CreatTeam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String TeamName1 = TeamName.getText().toString();// 队名
                String TeamCode1 = TeamCode.getText().toString();// 暗号
                Mana_id = MyApplication.getMana_id();
                //判断用户名和密码是否为空
                if (TextUtils.isEmpty(TeamName1.trim())
                        || TextUtils.isEmpty(TeamCode1.trim())) {
                    Toast.makeText(CreateTeam.this, "队名或者暗号不能为空", Toast.LENGTH_SHORT).show();
                }
                else if(TeamName1.matches(".{20}"))
                {
                    Toast.makeText(CreateTeam.this, "队名不能过长", Toast.LENGTH_SHORT).show();
                }
                else if(TeamCode1.matches(".{20}"))
                {
                    Toast.makeText(CreateTeam.this, "暗号不能过长", Toast.LENGTH_SHORT).show();
                }
                else {
                    // 发送请求给服务器
                    //调用：loginByAsyncHttpClientPost(userName, userPass);
                    loginByAsyncHttpClientPost(Mana_id,TeamName1, TeamCode1);
                }
            }
        });

    }

    public void loginByAsyncHttpClientPost(String mana_id,String TeamName1, String TeamCode1) {
        AsyncHttpClient client = new AsyncHttpClient(); // 创建异步请求的客户端对象
        // 创建请求参数的封装的对象
        RequestParams params = new RequestParams();
        params.put("Mana_id",mana_id);// 设置请求的参数名和参数值
        params.put("Team_name", TeamName1); // 设置请求的参数名和参数值
        params.put("code", TeamCode1);// 设置请求的参数名和参数

        // 执行post方法
        RequestHandle post = client.get(url, params, new AsyncHttpResponseHandler() {
            /**
             * 成功处理的方法
             * responseBody:响应内容的字节
             */
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  byte[] responseBody) {
                if (statusCode == 200) {
                    String str = new String(responseBody);
                    try {
                        JSONObject jsonObject = new JSONObject(str);
                        String code = jsonObject.getString("code");
                        if (code.equals("0")) {
                            Toast.makeText(CreateTeam.this, "创建球队成功", Toast.LENGTH_LONG).show();
                            startActivity(new Intent(CreateTeam.this, MainActivity.class));
                        } else {
                            Toast.makeText(CreateTeam.this, "创建球队失败", Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
            /**
             * 失败处理的方法
             * error：响应失败的错误信息封装到这个异常对象中
             */
            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  byte[] responseBody, Throwable error) {
                error.printStackTrace();// 把错误信息打印出轨迹来
                //Toast.makeText(MyApplication.getContext(), "网络错误", Toast.LENGTH_LONG).show();
            }
        });
    }
}